using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace ScytheEngine
{
    public class ScytheEngineGame : Microsoft.Xna.Framework.Game
    {
        #region Data Members

        private GraphicsDeviceManager m_GfxDevManager;
        private ContentManager m_ContentManager;
        private SceneManager m_SceneManager;
        private Input m_Input;
        private GameStateManager m_GameStateManager;

        #endregion

        #region Ctors

        public ScytheEngineGame()
        {
            m_GfxDevManager = new GraphicsDeviceManager((Game)this);
            m_ContentManager = new ContentManager(Services);
            m_SceneManager = new SceneManager();
            m_Input = new Input(this);
            m_GameStateManager = new GameStateManager(this);

            this.Components.Add(m_Input);
            this.Components.Add(m_GameStateManager);

            Window.AllowUserResizing = true;
			Window.ClientSizeChanged += new System.EventHandler( Window_ClientSizeChanged );
        }

        #endregion

        #region Methods

		protected override void Initialize()
		{
			ShaderManager.Initialize( m_GfxDevManager.GraphicsDevice );

			Camera defaultCamera = new Camera( m_GfxDevManager.GraphicsDevice.Viewport );
			CameraManager.AddCamera( defaultCamera, "default" );
			CameraManager.SetActiveCamera( "default" );

			base.Initialize();
		}

        protected override void Draw(GameTime gameTime)
        {
            // Clear the back buffer to cornflower blue
            m_GfxDevManager.GraphicsDevice.Clear(Color.CornflowerBlue);
            
            // Draw our scene (recurse down from the root)
            m_SceneManager.Root.Draw(m_GfxDevManager.GraphicsDevice);
            
            base.Draw(gameTime);
        }

		protected override void Update( GameTime gameTime )
		{
			//CameraManager.ActiveCamera.Update();
			base.Update( gameTime );
		}

        protected override void LoadContent()
        {
            m_SceneManager.Root.LoadContent(m_GfxDevManager.GraphicsDevice, m_ContentManager);
            ShaderManager.LoadContent(m_ContentManager);

            base.LoadContent();
        }

        #endregion

		#region Event Handlers

		void Window_ClientSizeChanged( object sender, System.EventArgs e )
		{
            m_GfxDevManager.PreferredBackBufferWidth = Window.ClientBounds.Width;
            m_GfxDevManager.PreferredBackBufferHeight = Window.ClientBounds.Height;

			CameraManager.UpdateViewports( m_GfxDevManager.GraphicsDevice.Viewport );
		}

		#endregion

		#region Properties

		public SceneManager SceneManager
        {
            get { return m_SceneManager; }
        }

        public Input Input
        {
            get { return m_Input; }
        }

        public GraphicsDeviceManager GraphicsDeviceManager
        {
            get { return m_GfxDevManager; }
        }

        public GameStateManager GameStateManager
        {
            get { return m_GameStateManager; }
        }

        #endregion
    }
}
